package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.R;
import com.example.entity.Bill;
import com.example.entity.Provider;
import com.example.entity.Role;
import com.example.entity.User;
import com.example.service.RoleService;
import com.example.service.UserService;
import com.example.vo.BillVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private UserService userService;
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize){
        log.info("page = {},pageSize = {}" ,page,pageSize);

        //构造分页构造器
        Page pageInfo = new Page(page,pageSize);

        //构造条件构造器
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper();
        //添加排序条件
        queryWrapper.orderByAsc(Role::getId);

        //执行查询
        roleService.page(pageInfo,queryWrapper);

        return R.success(pageInfo);
    }

    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Role role){

        log.info("新增角色信息：{}",role.toString());


        //获得当前登录用户的id
        Long userId = (Long) request.getSession().getAttribute("user");

        role.setCreatedby(userId);
        role.setCreationdate(LocalDateTime.now());

        roleService.save(role);

        return R.success("新增角色成功");
    }

    @GetMapping("/all/rolecode")
    public R<List<String>> getAllRolecodes(){
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();

        //选择要查询的字段
        queryWrapper.select(Role::getRolecode);
        

        //执行查询
        List<Object> rolecodesObjs = roleService.listObjs(queryWrapper);
        List<String> rolecodes = new ArrayList<>();
        for (Object pronameObj : rolecodesObjs) {
            rolecodes.add(String.valueOf(pronameObj));
        }

        return R.success(rolecodes);
    }

    /**
     * 根据id修改员工信息
     * @param role
     * @return
     */
    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Role role){

        log.info(role.toString());

        long id = Thread.currentThread().getId();
        log.info("线程id为：{}",id);
        Long userId = (Long) request.getSession().getAttribute("user");
        role.setModifyby(userId);
        role.setModifydate(LocalDateTime.now());
        roleService.updateById(role);

        return R.success("供应商信息修改成功");
    }

    /**
     * 根据id查询员工信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Role> getById(@PathVariable Long id){
        log.info("根据id查询供应商信息...");
        Role role = roleService.getById(id);
        if(role != null){
            return R.success(role);
        }
        return R.error("没有查询到对应供应商信息");
    }

    /**
     * 根据id删除供应商
     * @param id
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long id) {
        log.info("删除角色，id为：{}", id);

        // 检查角色是否被系统内用户引用
        List<User> userList = userService.list(new QueryWrapper<User>().eq("userrole", id));
        if (!userList.isEmpty()) {
            return R.error("角色被系统内用户所引用，无法删除！");
        }

        // 删除角色信息
        roleService.removeById(id);

        return R.success("角色信息删除成功");
    }

}